package com.zhlab.mybatisdemo.chapter3;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;

/**
 * @ClassName Chapter3DemoApplication
 * @Description //Chapter3DemoApplication
 * @Author singleZhang
 * @Email 405780096@qq.com
 * @Date 2020/12/29 0029 下午 4:34
 **/
@SpringBootApplication
public class Chapter3DemoApplication {

    public static void main (String[] args){
        //SpringApplication.run(Chapter3DemoApplication.class,args);

        // 第一阶段：MyBatis的初始化阶段
        String resource = "mybatis-config.xml";

        // 得到配置文件的输入流
        InputStream inputStream = null;
        try{
            inputStream = Resources.getResourceAsStream(resource);
        }catch (IOException e){
            e.printStackTrace();
        }

        // 得到SqlSessionFactory
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);


        // 第二阶段：数据读写阶段
        try(SqlSession session = sqlSessionFactory.openSession()){

            // 找到接口对应的实现
            SysAdminUserMapper userMapper = session.getMapper(SysAdminUserMapper.class);

            // 查询参数
            SysAdminUser userParam = new SysAdminUser();
            userParam.setDeptId(5);

            // 调用接口展开数据库操作
            List<SysAdminUser> userList = userMapper.getUserList(userParam);

            for (SysAdminUser user : userList) {
                System.out.println("name : " + user.getNickName() + " ;  email : " + user.getEmail());
            }
        }
    }

}
